﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.BasicDataManage
{
    public class FlowService : DbHelperBase
    {
        public IList<FlowEntity> GetFlowListByCombobox(string id)
        {
            var list = new List<FlowEntity>();
            var strSql = new StringBuilder();
            strSql.Append(" SELECT FlowID,ProcessIdx FROM UT_Flow where PrdID=@PrdID ");
            strSql.Append(" order by ProcessIdx ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@PrdID", id);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model=new FlowEntity();
                            if (reader["FlowID"] != DBNull.Value)
                                model.FlowID = new Guid(reader["FlowID"].ToString());
                            if (reader["ProcessIdx"] != DBNull.Value)
                                model.ProcessIdx = Convert.ToInt32(reader["ProcessIdx"].ToString());
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }

        public Guid GetFlowID(string prdCode, int processIdx)
        {
            var flowID = new Guid();
            var strSql = new StringBuilder();
            strSql.Append(" SELECT A.FlowID FROM UT_Flow AS A ");
            strSql.Append(" INNER JOIN UT_Product AS B ON A.PrdID=B.PrdID ");
            strSql.Append(" WHERE B.PrdCode=@PrdCode AND A.ProcessIdx=@ProcessIdx ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {

                try
                {
                    Db.AddInParameter(cmd, "@PrdCode", prdCode);
                    Db.AddInParameter(cmd, "@ProcessIdx", processIdx);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (reader["FlowID"] != DBNull.Value)
                                flowID = new Guid(reader["FlowID"].ToString());
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return flowID;
            }
        }
    }
}
